{extend name="base"/}
{block name="resources"}
<style type="text/css">
	.ns-form {margin-top: 0;}
	.tree-line{padding:10px 0;background:#ededed;margin-bottom:2px;line-height: 1.8;}
	.tree-line .layui-form{padding-left: 10px !important;}
	.tree-line .layui-form-checkbox{margin:0 10px !important;vertical-align:middle;}
	.ns-form {margin-top: 0;}
	.ns-item-block-hover-a {cursor: pointer;}
	.ns-input-box {position: absolute; right: 0; top: 50%; transform: translateY(-50%);}
	.layui-form-item .layui-form-checkbox[lay-skin=primary] {margin-top: 0;}
	.ns-item-block-parent .ns-item-block-wrap {padding-right: 35px;}
	.ns-item-content-desc{white-space: nowrap;overflow: hidden;text-overflow: ellipsis;}
</style>
{/block}
{block name="main"}
<div class="layui-form ns-form">
	
	<div class="layui-form-item">
		<label class="layui-form-label"><span class="required">*</span>套餐名称：</label>
		<div class="layui-input-block">
			<input name="group_name" type="text" required lay-verify="required" class="layui-input ns-len-long">
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label img-upload-lable">封面：</label>
		<div class="layui-input-block img-upload">
			<div class="upload-img-block icon">
				<div class="upload-img-box">
					<div class="ns-upload-default" id="group_image">
						<div class="upload">
							<img src="__STATIC__/img/upload_img.png" />
							<p>点击上传</p>
						</div>
					</div>
					<div class="operation"  >
						<div >
							<i title="图片预览" class="iconfont iconreview js-preview" style="margin-right: 20px;"></i>
							<i title="删除图片" class="layui-icon layui-icon-delete js-delete"></i>
						</div>
						<div class="replace_img js-replace">点击替换</div>
					</div>
					<input type="hidden" name="group_image"/>
				</div>
			</div>
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label"><span class="required">*</span>免费赠送短信：</label>
		<div class="layui-input-block">
			<input name="sms_num" type="number" required lay-verify="required" class="layui-input ns-len-short"/>
		</div>
	</div>

	<div class="layui-form-item distribution-index">
		<label class="layui-form-label"><span class="required">*</span>设置价格：</label>
		<div class="layui-input-block">
			<div class="layui-input-inline">
				<input type="number" name="fee" class="layui-input" value="0" required lay-verify="price"/>
			</div>
			<div class="layui-form-mid layui-word-aux">元/</div>
			<div class="layui-input-inline">
				<select name="unit" lay-filter="unit">
					<option value="week">周</option>
					<option value="month">月</option>
					<option value="year">年</option>
				</select>
			</div>
		</div>
	</div>

	<div class="layui-form-item distribution-index">
		<label class="layui-form-label">是否上架：</label>
		<div class="layui-input-block">
			<div class="layui-input-inline">
				<input type="radio" name="status" value="1" lay-filter="level" title="上架"/>
				<input type="radio" name="status" value="0" lay-filter="level" title="下架" checked/>
			</div>
		</div>
	</div>

	<div class="layui-form-item layui-form-text" id="promotion_list">
		<div id="tool">
			<span class="layui-form-label">应用管理：</span>
			<div class="layui-input-block">
				<div class="site_list ns-item-block-parent ns-item-five" >
					{foreach $addons as $list_k => $list_v}
					<a class="ns-item-block ns-item-block-hover-a">
						<div class="ns-item-block-wrap">
							<div class="ns-item-pic">
								<img src="{:img($list_v.icon)}" />
							</div>
							<div class="ns-item-con">
								<div class="ns-item-content-title">{$list_v.title}</div>
								<p class="ns-item-content-desc">{$list_v.description}</p>
							</div>
						</div>
						<div class="ns-input-box">
							<input type="checkbox" value="{$list_v.name}" class="ns-input-checkbox" lay-filter="is_checked" lay-skin="primary">
						</div>
					</a>
					{/foreach}
				</div>
			</div>
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">描述：</label>
		<div class="layui-input-block">
			<textarea name="desc" class="layui-textarea ns-len-long"></textarea>
		</div>
	</div>
	
	<!-- 表单操作 -->
	<div class="ns-form-row">
		<button class="layui-btn ns-bg-color" lay-submit lay-filter="save">保存</button>
		<button class="layui-btn layui-btn-primary" onclick="back()">返回</button>
	</div>
</div>

{/block}
{block name="script"}
<script>
	layui.use(['form'], function() {
		var form = layui.form,
			repeat_flag = false;//防重复标识
		form.render();

		var upload = new Upload({
			elem: '#group_image',
			post:"admin"
		});

        form.on('submit(save)', function (data) {

			var obj = $("#promotion_list input:checked");
			var addon_array = [];
			for (var i = 0; i < obj.length; i++) {
				addon_array.push(obj.eq(i).val());
			}
			data.field.addon_array = addon_array.toString();

			// 删除图片
			if(!data.field.group_image) upload.delete();

            if (repeat_flag) return;
            repeat_flag = true;
			
            $.ajax({
                type: "POST",
                dataType: "JSON",
                url: ns.url("admin/sitegroup/add"),
                data: data.field,
                success: function (res) {
					repeat_flag = false;
					if (res.code == 0) {
						layer.confirm('添加成功', {
							title:'操作提示',
							btn: ['返回列表', '继续添加'],
							closeBtn: 0,
							yes: function(){
								location.href = ns.url("admin/sitegroup/lists")
							},
							btn2: function() {
								location.href = ns.url("admin/sitegroup/add")
							}
						})
					}else{
						layer.msg(res.message);
					}
                }
            });
        });

        form.verify({
            title: function (value) {
                if (value.length == 0) {
                    return '请输入用户组名称';
                }
            },
			flnum: function (value, item) {
				var str = $(item).parents(".layui-block").find("span").text();
				var _str = $(item).siblings().val();
				var arrMen = value.split(".");
				var val = 0;
				if (arrMen.length == 2) {
					val = arrMen[1];
				}
				if (val.length > 2) {
					if (str) {
						return str + "最多可保留两位小数";
					} else {
						return _str + "最多可保留两位小数";
					}
				}
			},
			number: function (value, item) {
				var str = $(item).parents(".layui-block").find("span").text();
				var _str = $(item).siblings().val();
				if (value < 0) {
					if (str) {
						return str + "不能小于0";
					} else {
						return _str + "不能小于0";
					}
				}
			},
			int: function (value, item) {
				var str = $(item).parents(".layui-block").find("span").text();
				var _str = $(item).siblings().val();
				if (value%1 != 0) {
					if (str) {
						return str + "必须为整数";
					} else {
						return _str + "必须为整数";
					}
				}
			},
			price: function (value,item) {
            	if(!parseFloat($(item).val()) && parseFloat($(item).val()) !=0){
            		return "价格不能为空";
				}

				if(parseFloat($(item).val()) <= 0){
					return "价格不能小于等于零,请重新输入";
				}
			},
			time: function (value,item) {
				if(!parseFloat($(item).val()) && parseFloat($(item).val()) !=0){
					return "时间不能为空";
				}

				if(parseFloat($(item).val()) <= 0){
					return "时间不能小于零,请重新输入";
				}
			},
        });
    });
	
	function back() {
		location.href = ns.url("admin/sitegroup/lists");
	}

</script>
{/block}